<?php

/**
 * ecshop ERP v1.0
 * ----------------------------------------------------------------------------
 * http://www.phpally.com
 * Jacklee的博客 致力于php技术
 * ----------------------------------------------------------------------------
 * 作者: Jacklee
 * 邮箱: jack349392900@gmail.com
 * 创建时间: 2013-11-18
 * 最后修改时间: 2013-11-18
 */

define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');
require_once(dirname(__FILE__) . '/includes/lib/lib_erp_common.php');
require_once(dirname(__FILE__) . '/includes/lib/lib_erp_finance.php');

if ($_REQUEST['act'] == 'change_is_valid')
{
	include('../includes/cls_json.php');
	$json  = new JSON;
	
	//检查是否有编辑仓库的权限
	if(!admin_priv('erp_finance_manage', '', false))
	{
		$result['error']=1;
		$result['message']=$_LANG['erp_no_permit'];
		die($json->encode($result));
	}
	

	$account_id=intval($_REQUEST['account_id']);
	$bank_account_info=get_bank_account_info($account_id);
	if(empty($bank_account_info))
	{
		$result['error']=3;
		$result['message']=$_LANG['erp_bank_account_not_exists'];
		die($json->encode($result));
	}
	else{
		
		$is_valid=$bank_account_info['is_valid'];
		if($is_valid==1)
		{
			$sql="update ".$ecs->table('erp_bank_account')." set is_valid='0' where account_id='".$account_id."'";
			$db->query($sql);
			
			$result['error']=0;
			$result['is_valid']=0;
			die($json->encode($result));
		}
		elseif($is_valid==0)
		{
			$sql="update ".$ecs->table('erp_bank_account')." set is_valid='1' where account_id='".$account_id."'";
			$db->query($sql);
			
			$result['error']=0;
			$result['is_valid']=1;
			die($json->encode($result));			
		}
	}
}

elseif($_REQUEST['act'] == 'add_bank_account')
{
  include('../includes/cls_json.php');
	$json  = new JSON;

	if(!admin_priv('erp_finance_manage', '', false))
	{
		$result['error']=1;
		$result['message']=$_LANG['erp_no_permit'];
		die($json->encode($result));
	}
 
  	$account_name=trim($_POST['bank_account_name']);
		$account_no=trim($_POST['bank_account_no']);

    $sql="insert into ".$ecs->table('erp_bank_account')." set account_name ='".$account_name."',account_no='".$account_no."',is_valid='1'";
	 
    $db->query($sql);
    $result['error']=0;
    die($json->encode($result));
}

elseif($_REQUEST['act'] == 'delete_bank_account')
{
	include('../includes/cls_json.php');
	$json  = new JSON;
	
	if(!admin_priv('erp_finance_manage', '', false))
	{
		$result['error']=1;
		$result['message']=$_LANG['erp_no_permit'];
		die($json->encode($result));
	}
	
	$account_id=intval($_REQUEST['account_id']);

	$bank_account_info=get_bank_account_info($account_id);
	if(empty($bank_account_info))
	{
		$result['error']=3;
		$result['message']=$_LANG['erp_bank_account_not_exist'];
		die($json->encode($result));
	}
	else{
		
		//是否可以删除
		$sql="select count(*) as num from ".$GLOBALS['ecs']->table('erp_gathering')." where account_id='".$account_id."'";
		$num=$db->getOne($sql);
		if($num>0)
		{
			$result['error']=1;
			$result['message']=$_LANG['erp_bank_account_has_gathering'];
			die($json->encode($result));			
		}
		
		$sql="select count(*) as num from ".$GLOBALS['ecs']->table('erp_payment')." where account_id='".$account_id."'";
		$num=$db->getOne($sql);
		if($num>0)
		{
			$result['error']=1;
			$result['message']=$_LANG['erp_bank_account_has_payment'];
			die($json->encode($result));			
		}		
		
		$sql="select count(*) as num from ".$GLOBALS['ecs']->table('erp_bank_account_record')." where account_id='".$account_id."'";
		$num=$db->getOne($sql);
		if($num>0)
		{
			$result['error']=1;
			$result['message']=$_LANG['erp_bank_account_has_bank_record'];
			die($json->encode($result));			
		}				
		

		$sql="delete from ".$ecs->table('erp_bank_account')." where account_id='".$account_id."'";
		$db->query($sql);
		
		$result['error']=0;
		die($json->encode($result));
	}
}


?>